package fr.oz.view.profile 
{
	import air.update.utils.VersionUtils;
	import caurina.transitions.properties.ColorShortcuts;
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import fr.digitas.flowearth.ui.colorpicker.ColorPicker;
	import fr.oz.core.datas.ProfilListDatas;
	import fr.oz.gui.profile.Colorpicker_FC;
	import fr.oz.gui.profile.ControlBarColor;
	import fr.oz.mobile.ui.profile.ControlBar;
	import fr.oz.view.profile.dragable.ui.ColorHandler;
	import fr.oz.view.profile.dragable.ui.ColorHandler_MC;
	/**
	 * ...
	 * @author Djim
	 */
	public class ColorSelectorView extends Sprite
	{
		private var bmpd:BitmapData;
		//private var bmp:Bitmap;
		private var colorpicker_mc:Colorpicker_FC;
		private var  customColor:uint;
		private var controlBar:ControlBar;
		
		private var color:XMLList;
		private var beginControl:ColorHandler;
		private var endControl:ColorHandler;
		private var initMinut:int;
		private var initHour:int;
		private var chooseBeginColor:Boolean = true;
		private var _colorHandlerTmp:ColorHandler;
		
		public function ColorSelectorView(p:ProfilListDatas) 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			color = p.color;
			initHour = int(p.time_hour);
			initMinut = int(p.time_minut);
			
			ColorShortcuts.init();
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			initGUI();
		}
		
		private function initGUI():void 
		{
			colorpicker_mc = new Colorpicker_FC();
			addChild(colorpicker_mc);
			
			bmpd = new BitmapData(colorpicker_mc.width, colorpicker_mc.height);
			bmpd.draw(colorpicker_mc);

			//bmp = new Bitmap(bmpd);
			//bmp.x = colorpicker_mc.x;
			//bmp.y = colorpicker_mc.y + colorpicker_mc.height;
			
			controlBar = new ControlBar(initHour, initMinut);
			controlBar.y = 570;
			addChild(controlBar);
			
			beginControl = new ColorHandler_MC();
			addChild(beginControl);
			beginControl.x = getColorHandlerPosX();
			beginControl.y = 570;
			controlBar.setPosX(beginControl.x, true);
			beginControl.hideName();
			
			endControl = new ColorHandler_MC();
			addChild(endControl);
			endControl.x = getColorHandlerPosX(false);
			endControl.y = 570;
			controlBar.setPosX(endControl.x, false);
			endControl.hideName();
			
			setTimeout(controlBar.setColor, 50, color.gradient.start);
			setTimeout(controlBar.setColor, 50, color.gradient.end, false);
			//controlBar.setColor(uint(color.gradient.start), false);
			
			beginControl.chooseColor(uint(color.gradient.start));
			endControl.chooseColor(uint(color.gradient.end));
			
			beginControl.iselected = true;
			Tweener.addTween(beginControl, {alpha:1, scaleX:1, scaleY:1, time:.5 } );
			
			colorpicker_mc.maskColorpicker.addEventListener(MouseEvent.MOUSE_DOWN, catchColor);
			
			endControl.addEventListener(MouseEvent.MOUSE_DOWN, dragControl);
			beginControl.addEventListener(MouseEvent.MOUSE_DOWN, dragControl);
		}
		
		private function dragControl(e:MouseEvent):void 
		{
			if ((e.currentTarget as ColorHandler) == beginControl) {
				chooseBeginColor = true;
				endControl.iselected = false;
				Tweener.addTween(endControl, {alpha:1, scaleX:1, scaleY:1, time:.5 } );
				(e.currentTarget as Sprite).startDrag(false, new Rectangle(30, 570, endControl.x - 90, 0));
			}else {
				chooseBeginColor = false;
				beginControl.iselected = false;
				Tweener.addTween(beginControl, {alpha:1, scaleX:1, scaleY:1, time:.5 } );
				(e.currentTarget as Sprite).startDrag(false, new Rectangle(beginControl.x + 60, 570, 390 - beginControl.x, 0));
			}
			_colorHandlerTmp = e.currentTarget as ColorHandler;
			_colorHandlerTmp.iselected = true;
			_colorHandlerTmp.addEventListener(Event.ENTER_FRAME, moveGradient);
			_colorHandlerTmp.hideName(1);
			stage.addEventListener(MouseEvent.MOUSE_UP, changeGradientDone);
		}
		
		
		private function changeGradientDone(e:MouseEvent):void 
		{
			_colorHandlerTmp.hideName();
			stage.removeEventListener(MouseEvent.MOUSE_UP, changeGradientDone);
			_colorHandlerTmp.removeEventListener(Event.ENTER_FRAME, moveGradient);
			Tweener.addTween(_colorHandlerTmp, {alpha:1, scaleX:1, scaleY:1, time:.5 } );
			_colorHandlerTmp.stopDrag();
		}
		
		/**
		 * Récupération de la fraction parcouru par le ColorHandler pour préciser la position à ControlBar
		 * @param	e
		 */
		private function moveGradient(e:Event):void 
		{
			//Pour le raffraichissement de l'heure en fonction de la position du curseur
			var percent:Number = (e.currentTarget.x -60) * 100 / 420;
			trace(percent+"%");
			_colorHandlerTmp.putName(controlBar.whatTimeItIs(percent));
			controlBar.setPosX(_colorHandlerTmp.x, chooseBeginColor);
		}
		
		private function handleUp(e:MouseEvent):void 
		{
			if (chooseBeginColor) {
				beginControl.iselected = false;
				endControl.iselected = true;
			}else {
				beginControl.iselected = true;
				endControl.iselected = false;
			}
			Tweener.addTween(beginControl, {alpha:1, scaleX:1, scaleY:1, time:.5 } );
			Tweener.addTween(endControl, {alpha:1, scaleX:1, scaleY:1, time:.5 } );
			stage.removeEventListener(MouseEvent.MOUSE_UP, handleUp);
			chooseBeginColor = !chooseBeginColor;
			
			removeEventListener(Event.ENTER_FRAME, showColor);
		}
		
		private function catchColor(e:MouseEvent):void 
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, handleUp);
			addEventListener(Event.ENTER_FRAME, showColor);
		}

		private function showColor(e:Event = null):void 
		{	
			var mX:int = colorpicker_mc.mouseX;
			var mY:int = colorpicker_mc.mouseY;
			customColor = bmpd.getPixel(mX, mY);
			
			controlBar.setColor(customColor, chooseBeginColor);
			if (chooseBeginColor) {
				beginControl.chooseColor(customColor);
			}else {
				endControl.chooseColor(customColor);
			}
			
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
		
			removeEventListener(Event.ENTER_FRAME, showColor);
			
			colorpicker_mc.maskColorpicker.removeEventListener(MouseEvent.MOUSE_DOWN, catchColor);
			removeChild(colorpicker_mc);
			colorpicker_mc = null;
			
			
			endControl.removeEventListener(MouseEvent.MOUSE_DOWN, dragControl);
			beginControl.removeEventListener(MouseEvent.MOUSE_DOWN, dragControl);
			beginControl.removeEventListener(Event.ENTER_FRAME, moveGradient);
			endControl.removeEventListener(Event.ENTER_FRAME, moveGradient);
			if (_colorHandlerTmp != null) {
				_colorHandlerTmp.removeEventListener(Event.ENTER_FRAME, moveGradient);
				_colorHandlerTmp = null;
			}
			
			removeChild(endControl);
			removeChild(beginControl);
			endControl = null;
			beginControl = null;
			
			bmpd.dispose();
			bmpd = null;
			
			removeChild(controlBar);
			controlBar = null;
		}
		
		/**
		 * Calcul de la position des ColorHandler en fonction du temps
		 * @param	isBegin
		 * @return
		 */
		private function getColorHandlerPosX(isBegin:Boolean = true):int
		{
			var posX : int;
			var length:int = 420;
			var percent:Number
			
			if (isBegin) {
				var beginMinut:Number = Number(color.gradient.start.@hour) * 60 + Number(color.gradient.start.@minut);
				trace("beginMinut " + beginMinut);
				percent = (controlBar.totalMinut - beginMinut) / 30;
			}else {
				var endMinut:Number = Number(color.gradient.end.@hour) * 60 + Number(color.gradient.end.@minut);
				trace("endMinut " + endMinut);
				percent = (controlBar.totalMinut - endMinut) / 30;
			}
			trace("percent " + percent);
			posX = 450 - (percent * 420);
			trace("posX : " + posX);
			return posX;
		}
		
		public function constructColor():XMLList
		{
			var str:String = "<colors>" + "\n";
			str += "	<gradient>"+"\n";
			str += "		<start hour=\"" + color.gradient.start.@hour + "\" minut=\"" + color.gradient.start.@minut + "\"><![CDATA[0x"+controlBar.colorBegin.toString(16)+"]]></start>" + "\n";
			str += "		<end hour=\"" + color.gradient.end.@hour + "\" minut=\"" + color.gradient.end.@minut + "\"><![CDATA[0x"+controlBar.colorEnd.toString(16)+"]]></end>" + "\n";
			str += "	</gradient>" + "\n";
			str += "</colors>" + "\n";
			
			trace(str);
			return new XMLList(str);
		}
	}

}